b = [1,2,3,54,5]

n = len(b)
print("n: ",n)
copy = []
for i in range(n):
    copy.append([b[i]])
print(copy)

L = list(range(100))
print(L)
print(type(L))

# 简单切片操作，好用
print(L[:10]) # 前十个

print(L[-10:]) # 后面十个

print(L[10:20]) # 前面11 -20个数 取出索引为10-19的20个数

print(L[:10:2]) # 前十个，每隔两个取一个

print(L[::5]) # 所有数，每5个取一个

print(L[:]) # 什么都不写，原样list

tuple1 = (1,2,3,4,5,5,"fsdfsd",True,False,"fdsfa","hello world")
tl = len(tuple1)
print(type(tuple1))

print(tuple1[:tl])

print(tuple1[:2]) # 前面俩个
print(tuple1[-4:]) # 后面四个

#-----------------------------------------------------------------------------------------
# 字符串也可以进行切片操作
# print('abcdefghigklmnopqrstuvwxyz'[:1]) 
# print('abcdefghigklmnopqrstuvwxyz'[-20:])
# print('abcdefghigklmnopqrstuvwxyz'[2:4]) # 取出索引是2-3的元素

print('abcdefghigklmnopqrstuvwxyz'[1:]) # 从下标为1的到最后一个
print('abcdefghigklmnopqrstuvwxyz'[:-1]) # 删除最后一个

def trim(s):
    # 去除前面的空格
    while len(s) > 0 and s[0] == ' ':
        s = s[1:]
    # 去除后面的空格
    while len(s) > 0 and s[-1] == ' ':
        s = s[:-1]
    return s

# 测试:
if trim('hello  ') != 'hello':
    print('测试失败!')
elif trim('  hello') != 'hello':
    print('测试失败!')
elif trim('  hello  ') != 'hello':
    print('测试失败!')
elif trim('  hello  world  ') != 'hello  world':
    print('测试失败!')
elif trim('') != '':
    print('测试失败!')
elif trim('    ') != '':
    print('测试失败!')
else:
    print('测试成功!')


